package com.fyzk.activity.news;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tyjc.lottery.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanxs11 on 17/4/21.
 */

public class NewsListAdapter extends BaseAdapter {

    private List<NewsListBean.DataBean> infos = new ArrayList<>();
    private Context context;

    public NewsListAdapter(Context context) {
        this.context = context;
    }


    @Override
    public int getCount() {
        return infos.size();
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Object getItem(int i) {
        return infos.get(i);
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ViewHolder holderView;
        NewsListBean.DataBean info = infos.get(i);
        if (view == null) {
            holderView = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.view_news_list_item_set, null);

            holderView.title = (TextView) view.findViewById(R.id.news_list_item_title);
            holderView.img = (ImageView) view.findViewById(R.id.news_list_item_img);
//            holderView.time = (TextView) view.findViewById(R.id.news_list_item_time);



            view.setTag(holderView);
        } else {
            holderView = (ViewHolder) view.getTag();
        }

//        holderView.img.setDefaultImageResId(R.drawable.news_default);
        holderView.title.setText(Html.fromHtml(info.getTitle()));
        if (info.getIcon_url() != null && !info.getIcon_url().equals("")) {
            holderView.img.setVisibility(View.VISIBLE);
            Glide.with(context).load(info.getIcon_url()).into(holderView.img);
        } else {
            holderView.img.setVisibility(View.GONE);
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Long time = Long.parseLong(info.getUpdate_time());
//        holderView.time.setText(format.format(new Date(time*1000)));

        return view;
    }

    public void setInfos(List<NewsListBean.DataBean> infos) {
        this.infos = infos;
        this.notifyDataSetChanged();
    }

    class ViewHolder {

        ImageView img;
        TextView title;
//        TextView time;


    }
}
